import re
from 考试系统.数据处理层.shuju import stu
#from shuju import stu
from 考试系统.数据处理层.shuju import tea
from 考试系统.数据处理层.shuju import Class
from 考试系统.数据处理层.shuju import topic
from 考试系统.数据处理层.shuju import paper

# -*- coding: utf-8 -*-
def check_syntax(code):
    try:
        compile(code, '<string>', 'exec')
        return True  # 代码无语法错误
    except SyntaxError as e:
        return str(e)  # 返回语法错误信息
def run_code(code):
    try:
        exec(code)
    except Exception as e:
        return str(e)  # 返回运行时错误信息
    return None  # 代码运行成功
def code_check(code):
    a = check_syntax(code)
    if not a:
        return a
    if run_code(code) is None:
        return True
    else:
        return run_code(code)
def id_in_check(id):#检测该学号是否注册
    check_id1,ent = id_check(id)
    if ent =="stu":
        return stu.is_student_id_exists(id)
    if ent=="tea":
        return tea.is_teacher_id_exists(id)
    return False
def number_in_check(number,id):#检测该手机号是否已经注册过
    check_id1, ent = id_check(id)
    if ent =="stu":
        return stu.is_phone_number_registered(number)
    if ent=="tea":
        return tea.is_phone_number_registered(number)
    return False
def password_check(password):
    pattern = r"^(?=.*[a-z])(?=.*[A-Z]).{8,}$"
    match = re.search(pattern, password)
    if match:
        return True
    else :
        return False
def number_check(number):
    pattern = r"^1\d{10}$"
    match = re.search(pattern, number)
    if match :
        return True
    else :
        return False
def name_check(name):
    pattern = r"^\w{3,16}$"
    match = re.search(pattern,name)
    if match:
        return True
    else:
        return False
def ues_in(id,number,password,name):#注册
    num_check = number_check(number)
    password_check1 = password_check(password)
    id_check1,ent=id_check(id)
    name_Check =name_check(name)
    if num_check and password_check1 and id_check1 and name_Check:
        if ent=="stu":
            stu.stu_install_zhuce(id,number,password,name)
            return True
        else:
            tea_data = (id,number,password," ",name)
            tea.create_teacher(tea_data)
            return True
    return False

def class_check(name):#学生选课时，是否有这个班级
    return Class.is_class_name_exists(name)
def class_teacher_check(id,name):#创建班级是，检测一下班级是否已经存在
    teacher_class_names=tea.get_teacher_class(id)
    teacher_class_names = str(teacher_class_names)
    if name in teacher_class_names:
        return True
    else:
        return False
def id_check(id):
    stu_pattern = r"^2022[0-9]{8}$"
    teacher_pattern = r"^zut[0-9]{5}$"
    stu_match = re.search(stu_pattern, id)
    teacher_match = re.search(teacher_pattern, id)
    if stu_match:
        return True,'stu'
    if teacher_match:
        return True,'tea'
    return False,' '
def gender_check(str):
    pattern = r"^(男|女)$"
    match = re.search(pattern, str)
    if match:
        return True
    else :
        return False
def check_Topic(id):
    return topic.check_question_exists(id)
def check_paper_is_in(name):
    paper.check_exam_name_exists(name)
def check_paper_name(name):
    pattern = r"^\w{3,16}$"
    match = re.search(pattern, name)
    if match:
        return True
    else:
        return False

if __name__ == '__main__':
    print(code_check('print('))